package com.workManager.service;

import com.workManager.mapper.TbPaymentPlanMapper;
import com.workManager.mapper.TbProjectPaymentMapper;
import com.workManager.pojo.TbPaymentPlan;
import com.workManager.pojo.TbProjectPayment;
import com.workManager.util.DateUtil;
import com.workManager.util.WebUtil;
import com.workManager.vo.PaymentPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by stran on 2017/11/1 0001.
 */
@Service
public class PaymentPlanServiceImpl implements PaymentPlanService{

    @Autowired
    private TbPaymentPlanMapper paymentPlanMapper;

    @Autowired
    private TbProjectPaymentMapper projectPaymentMapper;

    public List<PaymentPlanVo> query(Map map) {
        return paymentPlanMapper.query(map);
    }

    public void add(TbPaymentPlan paymentPlan) {
        if(null!=paymentPlan.getRemark()&&!"".equals(paymentPlan.getRemark())){
            paymentPlan.setRemark("["+ DateUtil.getDate()+":"+ WebUtil.getCurrentUserVO().getUserName()+"]:"+paymentPlan.getRemark());
        }
        paymentPlanMapper.add(paymentPlan);
    }

    public void makeInvoice(TbPaymentPlan paymentPlan) {
        Date date = new Date();
        TbProjectPayment projectPayment = new TbProjectPayment();
        projectPayment.setAllocationAmount(paymentPlan.getAllocationAmount());
        projectPayment.setAllocationRatio(paymentPlan.getAllocationRatio());
        projectPayment.setAllocationType(paymentPlan.getAllocationType());
        projectPayment.setBillingAmount(paymentPlan.getBillingAmount());
        projectPayment.setBillingDate(paymentPlan.getBillingDate());
        projectPayment.setCreateDate(date);
        String remark = "";
        if(null!=paymentPlan.getRemark()&&!"".equals(paymentPlan.getRemark())) {
            String remarkArr[] = paymentPlan.getRemark().split("\\|");
            for (int i = 0; i < remarkArr.length; i++) {
                if (i == remarkArr.length - 1) {
                    remark += "[" + DateUtil.getDate() + ":" + WebUtil.getCurrentUserVO().getUserName() + "]:" + remarkArr[i];
                } else {
                    remark += remarkArr[i] + "|";
                }
            }
        }
        projectPayment.setDescription(remark);
        projectPayment.setIsValid(true);
        projectPayment.setNoTaxPayment(String.valueOf(paymentPlan.getNoTaxPayment()));
        projectPayment.setPaymentAmount(BigDecimal.valueOf(0));
        projectPayment.setPaymentType(paymentPlan.getPaymentType());
        projectPayment.setProjectId(paymentPlan.getProjectId());
        projectPayment.setTaxRate(paymentPlan.getTaxRate());
        projectPaymentMapper.insert(projectPayment);

        Map map = new HashMap();
        map.put("paymentId", projectPayment.getPaymentId());
        map.put("remark", remark);
        map.put("id", paymentPlan.getId());
        paymentPlanMapper.makeInvoice(map);
    }

    public void invalid(TbPaymentPlan paymentPlan) {
        Map map = new HashMap();
        String remarkArr[] = paymentPlan.getRemark().split("\\|");
        String remark = "";
        for(int i=0;i<remarkArr.length;i++){
            if(i==remarkArr.length-1){
                remark += "["+ DateUtil.getDate()+":"+ WebUtil.getCurrentUserVO().getUserName()+"]:"+remarkArr[i];
            }else{
                remark += remarkArr[i]+"|";
            }
        }
        map.put("remark", remark);
        map.put("id", paymentPlan.getId());
        paymentPlanMapper.invalid(map);
    }
}
